package hhh.app.data.net;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import hhh.app.common.rxjava.RxResultHelper;
import hhh.app.common.rxjava.SchedulersCompat;
import hhh.app.data.bean.Cart;
import hhh.app.data.bean.Category;
import hhh.app.data.bean.Product;
import hhh.app.data.bean.ProductDiscount;
import hhh.app.data.bean.ProductImage;
import hhh.app.data.bean.ProductOption;
import hhh.app.data.bean.Review;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
 * Created by hhh on 2016/12/6.
 */
public class ApiImp{
    private static final int CACHE_SIZE=10*1024*1024;
    private String url;
    private Context context;
    private Gson gson;
    private IApiService iService;
    private Cache cache;
    private OkHttpClient okHttpClient;
    private Retrofit retrofit;

    public ApiImp(Context context,String url) {
        this.url = url;
        this.context=context;
        gson=new GsonBuilder()
                .setDateFormat("yyyy-MM-dd HH:mm:ss")
                .create();
        cache=new Cache(context.getCacheDir(),CACHE_SIZE);
        okHttpClient=new OkHttpClient.Builder()
                .cache(cache)
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();
        retrofit=new Retrofit.Builder()
                .baseUrl(url)
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        iService =retrofit.create(IApiService.class);
    }

//    public Observable<List<Product>> getProductList(int page,int num,int category,String word,String sort){
//        return iService.getProducts(page,num,category,word,sort)
//                .compose(RxResultHelper.<List<Product>>handleResult())
//                .compose(SchedulersCompat.<List<Product>>io_main());
//    }

    public Observable<List<Product>> getProductList(Map<String,String> map){
        return iService.getProducts(map)
                .compose(RxResultHelper.<List<Product>>handleResult())
                .compose(SchedulersCompat.<List<Product>>io_main());
    }

    public Observable<Product> getProductDetail(int id){
        return iService.getProductById(id)
                .compose(RxResultHelper.<Product>handleResult())
                .compose(SchedulersCompat.<Product>io_main());
    }

    public Observable<List<Category>> getCategories(){
        return iService.getCategories()
                .compose(RxResultHelper.<List<Category>>handleResult())
                .compose(SchedulersCompat.<List<Category>>io_main());
    }

    public Observable<Category> getCategory(int id){
        return iService.getCategory(id)
                .compose(RxResultHelper.<Category>handleResult())
                .compose(SchedulersCompat.<Category>io_main());
    }

    public Observable<List<Cart>> getCarts(int uid){
        return iService.getCarts(uid)
                .compose(RxResultHelper.<List<Cart>>handleResult())
                .compose(SchedulersCompat.<List<Cart>>io_main());
    }

    public Observable<List<ProductImage>> getProductImage(int pid){
        return iService.getProductImages(pid)
                .compose(RxResultHelper.<List<ProductImage>>handleResult())
                .compose(SchedulersCompat.<List<ProductImage>>io_main());
    }

    public Observable<List<ProductDiscount>> getProductDiscount(int pid){
        return iService.getProductDiscounts(pid)
                .compose(RxResultHelper.<List<ProductDiscount>>handleResult())
                .compose(SchedulersCompat.<List<ProductDiscount>>io_main());
    }

    public Observable<List<ProductOption>> getProductOption(int pid){
        return iService.getProductOptions(pid)
                .compose(RxResultHelper.<List<ProductOption>>handleResult())
                .compose(SchedulersCompat.<List<ProductOption>>io_main());
    }

    public Observable<List<Review>> getProductReviews(int pid){
        return iService.getProductReviews(pid)
                .compose(RxResultHelper.<List<Review>>handleResult())
                .compose(SchedulersCompat.<List<Review>>io_main());
    }
}
